<?php


namespace app\astrologer\controller;


use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\common\cachemodel\ZlcrAstr;
use app\common\lib\exception\ApiException;
use think\Log;

class AstrAuth extends Common
{
    protected $astrInfo = null;
    // 准有好事 咨询师信息
    protected $consultAccountInfo = null;
    // 是否是 准有好事 咨询师
    protected $isConsultAstro = false;
    protected $errermsg = '';
    protected $is_simulated = false;
    protected $app_type = 'PC';


    public function _initialize()
    {
        parent::_initialize();

        if (!$this->isLogin())
        {
            throw new ApiException($this->errermsg, 200, config('code.denied'));
        }

        $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($this->astrInfo['id']);
        if ($consultAccountInfo)
        {
            $this->isConsultAstro = true;

            if ($consultAccountInfo['level_score'] <= -10)
            {   // 能力分 小于 -10  冻结接单资格
                $consultAccountInfo['extend']['question_pause_time'] = 2147483647;
            }
        }

        $this->consultAccountInfo = $consultAccountInfo;
    }

    private function isLogin()
    {
        if (isset($this->headers['app-type']))
        {
            $this->app_type = $this->headers['app-type'];
        }
        if (!isset($this->headers['access-token']) || empty($this->headers['access-token'])) throw new ApiException('前往登录', 200, 405);
        // access-token AES解密
        $accessUserToken = $this->decryptWithOpenssl($this->headers['access-token']);

        if (empty($accessUserToken)) throw new ApiException('前往登录', 200, 405);

        if (!preg_match('/||/', $accessUserToken))
        {
            $this->errermsg = '您的账户登录过期，请重新登录。';
            return false;
        }

        [$token, $id] = explode('||', $accessUserToken);

        // 账号模拟
        $simulation_astrologer = config('simulation_astrologer');
        if ($simulation_astrologer && $simulation_astrologer['is_open'] === 1)
        {// 打开模拟的状态
            if ((int)$id === (int)$simulation_astrologer['from'])
            {// 如果当前访问的占星师id 和配置中一样的话，那么开始模拟
                if ($simulation_astrologer['to'])
                {
                    $id = $simulation_astrologer['to'];
                    $this->is_simulated = true;
                } else
                {
                    Log::write('占星师后台AstrAuth模拟失败，to为空', 'error', true);
                }
            }
        }

        $astrModel = new ZlcrAstr();

        $astrInfo = $astrModel->getInfoById($id);

        if (!$astrInfo)
        {
            throw new ApiException('前往登录', 200, 405);
        }
        if (!$this->is_simulated)
        {
            if ($token !== $astrInfo['token'])
            {
                $this->errermsg = '当前账号登录过期，请您重新登录。';
                return false;
            }
        }

        if (!$astrInfo['status'])
        {
            $this->errermsg = '您的账户登录过期，请重新登录。';
            return false;
        }

        // 判定时间是否过期
        if ($this->is_simulated === false && time() > $astrInfo['token_expire'])
        {
            $this->errermsg = '您的账户登录过期，请重新登录。';
            return false;
        }

        $astrInfo['id'] = (int)$astrInfo['id'];
        $astrInfo['proportion'] = $astrModel->astrLevelProportion[abs($astrInfo['astr_level'])] ?? 0.1;
        $this->astrInfo = $astrInfo;

        /**
         * 定义全局常量 UID 方便日志打印带上用户ID信息
         */
        if (!defined('USERINFO_UID'))
        {
            define('USERINFO_UID', 'astrId:' . $astrInfo['id']);
        }

        return true;
    }
}
